package com.softline.documentsmall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softline.documentsmall.entity.Collection;
import com.softline.documentsmall.entity.CollectionVO;
import com.softline.documentsmall.entity.SpecialDictionaryVO;
import com.softline.documentsmall.entity.ThumbsUp;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.CollectionService;
import com.softline.documentsmall.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: documentsmall
 * @description:
 * @author: wmj
 * @create: 2020-12-09
 * @ClassName: CollectionController
 */

@Api(value="收藏相关接口",tags={"收藏相关接口"})
@RestController
@RequestMapping("/collection")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class CollectionController {

    @Autowired
    CollectionService collectionService;
    @Autowired
    DictionaryService dictionaryService;

    @ApiOperation(value="个人中心收藏列表页", notes = "个人中心收藏列表页")
    @GetMapping(value = "/listByPersonal")
    public ReturnVO listByPersonal(@RequestParam(name = "passportId")String passportId,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,@RequestParam(name = "type",defaultValue = "en")String type){
        QueryWrapper<Collection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passport_id",passportId);
        Page<CollectionVO> result = new Page<>();
        List<CollectionVO> records = collectionService.getReconds(passportId,type,pageNo,pageSize);
        Long count = Long.valueOf(collectionService.count(queryWrapper));
        result.setRecords(records);
        result.setCurrent(pageNo);
        result.setSize(pageSize);
         result.setTotal(count);
        return ReturnVOUtils.success(result);
    }
    
    @ApiOperation(value="收藏", notes = "收藏")
    @PostMapping(value = "/add")
    public ReturnVO add(@RequestBody Collection collection){
        int add = collectionService.add(collection);
        if(add == 1){
            return ReturnVOUtils.success("收藏成功");
        }else {
            return ReturnVOUtils.businessFailCustom(201,"您已收藏过该词条");
        }
    }

    @ApiOperation(value="根据词条id和用户id取消收藏记录", notes = "根据词条id和用户id取消收藏记录")
    @DeleteMapping(value = "/delete")
    public ReturnVO delete(@RequestParam("passportId") String passportId,@RequestParam("dictionaryId") Integer dictionaryId){
        collectionService.delete(passportId,dictionaryId);
        return ReturnVOUtils.success("取消收藏成功");
    }

    @ApiOperation(value="根据id删除收藏记录", notes = "根据id删除收藏记录")
    @DeleteMapping(value = "/deleteById")
    public ReturnVO deleteById(@RequestParam("id") Integer id){
        collectionService.removeById(id);
        return ReturnVOUtils.success("取消收藏成功");
    }

    @ApiOperation(value="根据词条id和用户查询是否收藏，1：收藏，0：未收藏", notes = "根据词条id和用户查询是否收藏，1：收藏，0：未收藏")
    @GetMapping(value = "/isCollection")
    public ReturnVO isCollection(@RequestParam("passportId") String passportId,@RequestParam("dictionaryId") Integer dictionaryId){
        Collection collection = collectionService.isCollection(passportId,dictionaryId);
        if (collection == null){
            return ReturnVOUtils.success("0");
        }else {
            return ReturnVOUtils.success("1");
        }
    }
}
